package com.example.movie.controller;

import com.example.movie.entity.TPlayRecord;
import com.example.movie.entity.TVideo;
import com.example.movie.service.TVideoService;
import com.example.movie.vo.PageResult;
import com.example.movie.vo.RestResult;
import com.example.movie.vo.RestResultBuilder;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


/**
 * (TVideo)控制层
 *
 * @author makejava
 * @since 2021-04-02 16:51:15
 */
@RestController
@RequestMapping("/tVideo")
@CrossOrigin
public class TVideoController {
    /**
     * 服务对象
     */
    @Resource
    private TVideoService tVideoService;

    /**
     * 通过主键查询单条数据
     *
     * @param tVideo 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public RestResult selectOne(TVideo tVideo) {
        TVideo result = tVideoService.selectById(tVideo.getVideoId());
        result.setPlayCount(result.getPlayCount()+1);
        tVideoService.update(result);
        if (result != null) {
            return new RestResultBuilder<>().success("查询成功", result);
        } else {
            return new RestResultBuilder<>().error("查询失败");
        }
    }

    /**
     * 新增一条数据
     *
     * @param tVideo 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public RestResult insert(@RequestBody TVideo tVideo) {
        int result = tVideoService.insert(tVideo);
        if (result > 0) {
            return new RestResultBuilder<>().success("新增成功", tVideo);
        }
        return new RestResultBuilder<>().error("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param tVideo 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public RestResult update(@RequestBody TVideo tVideo) {
        TVideo result = tVideoService.update(tVideo);
        if (result != null) {
            return new RestResultBuilder<>().success("修改成功", result);
        }
        return new RestResultBuilder<>().error("修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param tVideo 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public RestResult delete(@RequestBody TVideo tVideo) {
        int result = tVideoService.deleteById(tVideo.getVideoId());
        if (result > 0) {
            return new RestResultBuilder<>().success("删除成功", null);
        }
        return new RestResultBuilder<>().error("删除失败");
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public RestResult selectAll() {
        List<TVideo> tVideos = tVideoService.selectAll();
        if (tVideos != null) {
            return new RestResultBuilder<>().success("查询成功", tVideos);
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public RestResult selectPage(Integer start, Integer limit , String typeId) {
        List<TVideo> tVideos = tVideoService.selectPage(start, limit);

        if (tVideos != null) {
            return new RestResultBuilder<>().success("查询成功", new PageResult<>(tVideos,Long.parseLong(String.valueOf( tVideoService.count()))));
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 分页条件查询
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectList", method = RequestMethod.POST)
    public RestResult selectList(TVideo tVideo) throws ParseException {
        Date showTime = tVideo.getShowTime();
        if(showTime!=null){
            if(tVideo.getShowTime().getTime()>new Date().getTime()){
                tVideo.setShowTime(null);
            }
        }

        if(tVideo.getTypeId()==""){
            tVideo.setTypeId(null);
        }
        if(tVideo.getVideoRegion()==""){
            tVideo.setVideoRegion(null);
        }
        List<TVideo> tVideos = tVideoService.selectList(tVideo);
        if (tVideos != null) {
            return new RestResultBuilder<>().success("查询成功", new PageResult<>(tVideos,Long.parseLong(String.valueOf( tVideoService.countList(tVideo)))));
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 按照分类查询电影
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectByTypeList", method = RequestMethod.POST)
    public RestResult selectByTypeList(TVideo tVideo) throws ParseException {
        List<TVideo> tVideos = tVideoService.selectList(tVideo);
        if (tVideos != null) {
            return new RestResultBuilder<>().success("查询成功", new PageResult<>(tVideos,Long.parseLong(String.valueOf( tVideoService.countList(tVideo)))));
        }
        return new RestResultBuilder<>().error("查询失败");
    }


    /**
     * 播放量
     *
     */
    @RequestMapping(value = "playCount", method = RequestMethod.POST)
    public void playCount(TVideo tVideo) {
        List<TVideo> tVideos = tVideoService.selectList(tVideo);
        for (int i = 0; i < tVideos.size(); i++) {
            TVideo tVideo1 = tVideos.get(i);
            tVideo1.setPlayCount(tVideo1.getPlayCount()+1);
            tVideoService.update(tVideo1);
        }
    }

    /**
     * 记录时长
     *
     */
    @RequestMapping(value = "movieTime", method = RequestMethod.POST)
    public void MovieTime(String time) {
        System.out.println("播放到了："+time);
    }

    /**
     * 获取首页热门电影
     * @return
     */
    @RequestMapping(value = "getHotMovie", method = RequestMethod.GET)
    public RestResult getHotMovie() {
        List<TVideo> tVideos = tVideoService.getHotMovie();
        if (tVideos != null) {
            return new RestResultBuilder<>().success("查询成功", tVideos);
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 猜你喜欢
     * @param tVideo
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "selectByLoveList", method = RequestMethod.POST)
    public RestResult selectByLoveList(TVideo tVideo) throws ParseException {
        List<TVideo> tVideos = tVideoService.getLove(tVideo);
        if (tVideos != null) {
            return new RestResultBuilder<>().success("查询成功", new PageResult<>(tVideos,Long.parseLong(String.valueOf( tVideoService.countList(tVideo)))));
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 搜索框
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectbyName", method = RequestMethod.POST)
    public RestResult selectbyName(TVideo tVideo) throws ParseException {
        List<TVideo> tVideos = tVideoService.selectByName(tVideo);
        if (tVideos != null) {
            return new RestResultBuilder<>().success("查询成功", new PageResult<>(tVideos,Long.parseLong(String.valueOf( tVideoService.countList(tVideo)))));
        }
        return new RestResultBuilder<>().error("查询失败");
    }

    /**
     * 记录播放记录
     * @param tPlayRecord
     */
    @RequestMapping(value = "addPlayRecord", method = RequestMethod.POST)
    public void addPlayRecord(TPlayRecord tPlayRecord){
        tVideoService.addTPlayRecord(tPlayRecord);
    }


    /**
     * 获取播放记录
     * @param tPlayRecord
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "getPlayRecord", method = RequestMethod.POST)
    public RestResult getPlayRecord(TPlayRecord tPlayRecord) throws ParseException {
        TPlayRecord tPlayRecord1 = tVideoService.getTPlayRecord(tPlayRecord);
        if (tPlayRecord1 != null) {
            return new RestResultBuilder<>().success("查询成功", tPlayRecord1);
        }
        return new RestResultBuilder<>().error("查询失败");
    }
}
